package cn.b.sky.mes.qm.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import cn.b.sky.mes.qm.model.Repairmethod;
import cn.b.sky.mes.qm.model.condition.RepairmethodCondition;
import cn.b.sky.mes.qm.service.RepairmethodService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.b.sky.common.constants.SystemConstant;
import cn.b.sky.common.exception.ImeException;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.model.DWZResult;
import cn.b.sky.user.controller.SecurityController;

/**
 * 
 * <strong>Title : RepairmethodController.java<br></strong>
 * <strong>Package : cn.b.sky.mes.qm.controller<br></strong>
 * <strong>Description : </strong>返修方法控制器<br> 
 * <strong>Create on : 2015-12-14 下午2:27:35<br></strong>
 * <p>
 * @author lidapeng<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Controller
@RequestMapping("/repairmethod/*")
public class RepairmethodController extends SecurityController {

  @Resource
  private RepairmethodService repairmethodService;

  @RequestMapping("repairmethodList")
  public String repairmethodList(@ModelAttribute("condition") RepairmethodCondition condition, String navTabId,
                                 Model model) {
    condition.setSiteCode(this.getSiteCode());
    List<Repairmethod> repairmethods = repairmethodService.getRepairmethodsByCondition(condition);
    int count = repairmethodService.getRepairmethodCountByCondition(condition);
    condition.setData(repairmethods);
    condition.setTotalCount(count);
    model.addAttribute("page", condition);
    model.addAttribute(NAVTABID, navTabId);
    return "qm/repairMethod/repairmethod_list";
  }

  @RequestMapping("addRepairmethod")
  public String addRepairmethod(String navTabId, Model model) {
    model.addAttribute(NAVTABID, navTabId);
    return "qm/repairMethod/repairmethod_add";
  }

  @RequestMapping("editRepairmethod")
  public String editRepairmethod(String repairmethodCode, String navTabId, Model model) {

    if (StringUtils.isBlank(repairmethodCode)) {
      throw ImeException.newException(SystemConstant.PARAMETERNULL);
    }
    Repairmethod repairmethod = repairmethodService.getRepairmethodBySiteCodeAndRepairmethodCode(this.getSiteCode(),
        repairmethodCode);

    model.addAttribute("repairmethod", repairmethod);
    model.addAttribute(NAVTABID, navTabId);

    return "qm/repairMethod/repairmethod_edit";
  }

  @RequestMapping("viewRepairmethod")
  public String viewRepairmethod(String repairmethodCode, String navTabId, Model model) {

    if (StringUtils.isBlank(repairmethodCode)) {
      throw ImeException.newException(SystemConstant.PARAMETERNULL);
    }
    Repairmethod repairmethod = repairmethodService.getRepairmethodBySiteCodeAndRepairmethodCode(this.getSiteCode(),
        repairmethodCode);

    model.addAttribute("repairmethod", repairmethod);
    model.addAttribute(NAVTABID, navTabId);

    return "qm/repairMethod/repairmethod_view";
  }

  @RequestMapping("delRepairmethod")
  @ResponseBody
  public DWZResult delRepairmethod(String ids, String navTabId, Model model, HttpServletRequest request) {
    if (StringUtils.isBlank(ids)) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    String[] idArr = ids.split(",");
    List<BatchJobReport> reports = new ArrayList<BatchJobReport>();
    int failures = 0;
    for (int i = 0; i < idArr.length; i++) {
      String message = repairmethodService
          .removeRepairmethodBySiteCodeAndRepairmethodCode(this.getSiteCode(), idArr[i]);
      if (!StringUtils.isBlank(message)) {
        failures += 1;
        BatchJobReport report = new BatchJobReport(idArr[i], this.getImeViewMessageI18n(this.getCurrentLanguage(),
            message), false, this.getCurrentLanguage(), this.getSiteCode());
        reports.add(report);
      } else {
        BatchJobReport report = new BatchJobReport(idArr[i], "", true, this.getCurrentLanguage(), this.getSiteCode());
        reports.add(report);
      }
    }
    if (failures == 0) {
      return navTabAjaxDone(navTabId, null, null, null,
          getImeViewMessageI18n(this.getCurrentLanguage(), "successfulOperation.M"), null);
    } else {
      return batchjobAjaxDone( request, reports);
    }
  }

  @RequestMapping("saveRepairmethod")
  @ResponseBody
  public DWZResult saveRepairmethod(Repairmethod repairmethod, String navTabId, Model model) {

    if (repairmethod == null || StringUtils.isBlank(repairmethod.getRepairmethodCode())) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    repairmethod.setSiteCode(this.getSiteCode());
    setCreateInfo(repairmethod);
    repairmethodService.saveRepairmethod(repairmethod);
    return dialogAjaxDone(navTabId, null, null, null);
  }

  @RequestMapping("modifyRepairmethod")
  @ResponseBody
  public DWZResult modifyRepairmethod(Repairmethod repairmethod, String navTabId, Model model) {

    if (repairmethod == null || StringUtils.isBlank(repairmethod.getRepairmethodCode())) {
      return ajaxError(getImeViewMessageI18n(null, SystemConstant.PARAMETERNULL));
    }
    repairmethod.setSiteCode(this.getSiteCode());
    setCreateInfo(repairmethod);
    repairmethodService.modifyRepairmethod(repairmethod);
    return dialogAjaxDone(navTabId, null, null, null);
  }

  @RequestMapping("isExsitsRepairmethodCode")
  @ResponseBody
  public Boolean isExsitsRepairmethodCode(String repairmethodCode) {
    if (StringUtils.isBlank(repairmethodCode)) {
      return true;
    }
    Repairmethod repairmethod = repairmethodService.getRepairmethodBySiteCodeAndRepairmethodCode(this.getSiteCode(),
        StringUtils.upperCase(repairmethodCode));
    if (repairmethod != null && !StringUtils.isBlank(repairmethod.getRepairmethodCode())) {
      return false;
    }
    return true;
  }

  @RequestMapping("repairmethodDropdown")
  @ResponseBody
  public List<Repairmethod> repairmethodDropdown(String repairmethodCode) {
    RepairmethodCondition condition = new RepairmethodCondition();
    condition.setSiteCode(getSiteCode());
    condition.setRepairmethodCode(repairmethodCode);
    condition.setLockFlag(0);
    return repairmethodService.getRepairmethodsForSuggest(condition);
  }

  @RequestMapping("checkRepairmethodCode")
  @ResponseBody
  public boolean checkRepairmethodCode(String repairmethodCode) {
    Repairmethod repairmethod = repairmethodService.getRepairmethodBySiteCodeAndRepairmethodCode(getSiteCode(),
        repairmethodCode);
    return repairmethod != null && repairmethod.getLockFlag() != 1;
  }

  @RequestMapping("repairmethodLookup")
  public String repairmethodLookup(@ModelAttribute("condition") RepairmethodCondition condition, String navTabId,
      Model model) {
    condition.setSiteCode(getSiteCode());
    condition.setLockFlag(0);
    List<Repairmethod> list = repairmethodService.getRepairmethodsByCondition(condition);
    int count = repairmethodService.getRepairmethodCountByCondition(condition);

    condition.setTotalCount(count);
    condition.setData(list);

    model.addAttribute(NAVTABID, navTabId);
    model.addAttribute("page", condition);
    return "qm/repairMethod/repairmethod_lookup";
  }
}
